package com.homer.gps.track;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockActivity;
import com.homer.gps.track.adapters.PlacesArrayAdapter;
import com.homer.gps.track.helper.AnimatedLinearLayout;
import com.homer.gps.track.helper.UsefullStaff;
import com.homer.gps.track.persist.DatabaseHelper;
import com.homer.gps.track.persist.PlaceModel;
import com.homer.gps.track.persist.Repository;
import com.j256.ormlite.android.apptools.OpenHelperManager;

public class Places extends SherlockActivity implements OnNavigationListener {

	private static final int PLACE_EDIT_SCREEN = 101;

	private ListView placesList;
	private PlacesArrayAdapter placesListAdapter;
	private Repository repository;
	private DatabaseHelper dbHelper = null;
	private AnimatedLinearLayout editToolbarLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.places);
		initActivity();
		editToolbarLayout = (AnimatedLinearLayout) findViewById(R.id.edit_toolbar);

		this.repository = new Repository(getHelper());
		this.placesListAdapter = new PlacesArrayAdapter(this, R.layout.places_list_item, repository.getPlaces());
		placesList = (ListView) findViewById(R.id.places_list);
		placesList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
				int itemId = placesListAdapter.getItem(position).getId();
				if (getIntent().getBooleanExtra("requestCode", false)) {
					Intent result = new Intent();
					result.putExtra("selectedItemId", itemId);
					setResult(Activity.RESULT_OK, result);
					finish();
				}else {
					Intent i = new Intent(Places.this, PlaceDetail.class);
					i.putExtra("id", itemId);
					startActivity(i);
				}
			} 
		});
		placesList.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				placesListAdapter.setDeleteMode(true);
				editToolbarLayout.show();
				return true;
			}
		});
		this.placesList.setAdapter(this.placesListAdapter);
	}

	@Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		switch (itemPosition) {
		case 0:
			Intent i = new Intent(this, Routes.class);
			startActivity(i);
			break;
		case 1:
			break;
		default:
			break;
		}
		return false;
	}

	private void initActivity() {
		Context context = getSupportActionBar().getThemedContext();
		ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.locations, R.layout.sherlock_spinner_item);
		list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		getSupportActionBar().setListNavigationCallbacks(list, this);
		getSupportActionBar().setSelectedNavigationItem(1);
		getSupportActionBar().setIcon(R.drawable.places);
		getSupportActionBar().setTitle(R.string.title_places);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
	}

	public void onNewPlaceClicked(View view) {
		Intent i = new Intent(this, PlaceEdit.class);
		startActivityForResult(i, PLACE_EDIT_SCREEN);
	}

	private DatabaseHelper getHelper() {
		if (dbHelper == null) {
			dbHelper = (DatabaseHelper)OpenHelperManager.getHelper(this, DatabaseHelper.class);
		}
		return dbHelper;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case PLACE_EDIT_SCREEN:
			if (resultCode == Activity.RESULT_OK) {
				updatePlacesList();
			}
			break;
		default:
			super.onActivityResult(requestCode, resultCode, data);
			break;
		}
	}
	public void updatePlacesList(){
		placesListAdapter.clear();
		for(PlaceModel place : repository.getPlaces()){
			placesListAdapter.add(place);
		}
	}

	public void onDeleteClicked(final View view) {
		new AlertDialog.Builder(this)
		.setTitle("Delete entry")
		.setMessage("Are you sure you want to delete this entry?")
		.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) { 
				repository.deletePlace((PlaceModel)view.getTag());
				updatePlacesList();
				dialog.dismiss();
			}
		})
		.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) { 
				dialog.dismiss();
			}
		})
		.show();
	}
	
	public void onCompleteClicked(View view){
		placesListAdapter.setDeleteMode(false);
		editToolbarLayout.hide();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		getSupportActionBar().setSelectedNavigationItem(1);
	}
	
	@Override
	protected void onRestart() {
		UsefullStaff.restart(this, null);
		super.onRestart();
	}
}
